package edu.thu.studio.ormdesigner.outline.dialog;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.thu.studio.ormdesigner.common.AbstractDialog;
import edu.thu.studio.ormdesigner.common.CompositeFactory;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.util.Properties;
import edu.thu.studio.ormdesigner.util.Utils;

public class DiagramDialog extends AbstractDialog {
    
    private Text codeText;
    private Text nameText;
    private Text commentText;
    private Diagram diagram;
    private Diagram result;
    
    public DiagramDialog(Shell parentShell, Diagram diagram) {
        super(parentShell);
        this.diagram = diagram;
    }
    
    @Override
    protected void initialize(Composite composite) {
        this.codeText = CompositeFactory.createText(this, composite, Properties.outline_label_code, false, true);
        this.nameText = CompositeFactory.createText(this, composite, Properties.outline_labeldiagram, false, true);
        this.commentText = CompositeFactory.createTextArea(this, composite, Properties.outline_label_comment, 300, 100,
            1, true);
    }
    
    @Override
    protected void setData() {
        if (null != this.diagram) {
            this.codeText.setText(this.diagram.getCode());
            this.nameText.setText(this.diagram.getName());
            this.commentText.setText(this.diagram.getComment());
        }
    }
    
    @Override
    protected String getErrorMessage() {
        String code = this.codeText.getText().trim();
        String name = this.nameText.getText().trim();
        if (code.equals("")) {
            return Properties.error_code_empty;
        }
        if (name.equals("")) {
            return Properties.errordiagram_name_empty;
        }
        return null;
    }
    
    @Override
    protected void perfomeOK() throws Exception {
        this.result = new Diagram();
        if (null != this.diagram) {
            // 编辑
            this.result.setId(this.diagram.getId());
        } else {
            // 创建
            this.result.setId(Utils.getUUID());
        }
        this.result.setCode(this.codeText.getText().trim());
        this.result.setName(this.nameText.getText().trim());
        this.result.setComment(this.commentText.getText().trim());
    }
    
    @Override
    protected String getTitle() {
        return Properties.outline_labeldiagram;
    }
    
    public Diagram getResult() {
        return result;
    }
    
}
